import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Calendar;

import org.rsbot.script.GEItemInfo;

public class MMerchantEXTREME {

	static int startPrice;

	static String read;
	static File file = new File("MMerchantFile.txt");;

	static BufferedWriter out;
	static BufferedReader in;

	static boolean success;
	static String[] temp = null;

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		if (!file.exists()) {
			success = file.createNewFile();
		}
		
		if (success) {
			readFile();
		} else {
			createFile();
		}

	}

	public static GEItemInfo loadItemInfo(final int itemID) {
		int minPrice = 0;
		int maxPrice = 0;
		int marketPrice = 0;
		String changeSeven = "";
		String changeThirty = "";

		try {
			final URL url = new URL(
					"http://itemdb-rs.runescape.com/viewitem.ws?obj=" + itemID);
			final BufferedReader reader = new BufferedReader(
					new InputStreamReader(url.openStream()));

			String line;

			while ((line = reader.readLine()) != null) {
				if (line.contains("Minimum price:")) {
					line = line.replace("<b>Minimum price:</b> ", "");
					line = line.replace(",", "");
					if (line.contains("k")) {
						line = line.replace(".", "");
						line = line.replace("k", "");
						line = line.trim();
						minPrice = Integer.parseInt(line) * 100;
					} else if (line.contains("m")) {
						line = line.replace(".", "");
						line = line.replace("m", "");
						line = line.trim();
						minPrice = Integer.parseInt(line) * 100000;
					} else {
						minPrice = Integer.parseInt(line);
					}
				}

				if (line.contains("Market price:")) {
					line = line.replace("<b>Market price:</b> ", "");
					line = line.replace(",", "");
					if (line.contains("k")) {
						line = line.replace(".", "");
						line = line.replace("k", "");
						line = line.trim();
						marketPrice = Integer.parseInt(line) * 100;
					} else if (line.contains("m")) {
						line = line.replace(".", "");
						line = line.replace("m", "");
						line = line.trim();
						marketPrice = Integer.parseInt(line) * 100000;
					} else {
						marketPrice = Integer.parseInt(line);
					}
				}

				if (line.contains("Maximum price:")) {
					line = line.replace("<b>Maximum price:</b> ", "");
					line = line.replace(",", "");
					if (line.contains("k")) {
						line = line.replace(".", "");
						line = line.replace("k", "");
						line = line.trim();
						maxPrice = Integer.parseInt(line) * 100;
					} else if (line.contains("m")) {
						line = line.replace(".", "");
						line = line.replace("m", "");
						line = line.trim();
						maxPrice = Integer.parseInt(line) * 100000;
					} else {
						maxPrice = Integer.parseInt(line);
					}
				}

				if (line.contains("7 Days:")) {
					// <b>7 Days:</b> <span class="rise">+4.3%</span>
					line = line.replace("<b>7 Days:</b> <span class=\"stay\">",
							"");
					line = line.replace("<b>7 Days:</b> <span class=\"stay\">",
							"");
					line = line.replace("</span>", "");
					line = line.trim();
					changeSeven = line;
				}

				if (line.contains("30 Days:")) {
					// <b>7 Days:</b> <span class="rise">+4.3%</span>
					line = line.replace(
							"<b>30 Days:</b> <span class=\"stay\">", "");
					line = line.replace(
							"<b>30 Days:</b> <span class=\"stay\">", "");
					line = line.replace("</span>", "");
					line = line.trim();
					changeThirty = line;
				}
			}
		} catch (final Exception e) {
		}

		return new GEItemInfo(itemID, minPrice, maxPrice, marketPrice,
				changeSeven, changeThirty);
	}

	// Creates the file
	public static void createFile() {
		try {
			Calendar now = Calendar.getInstance();
			int month = now.get(Calendar.MONTH);
			int day = now.get(Calendar.DAY_OF_MONTH);

			GEItemInfo money = loadItemInfo(10476);
			int price = money.getMarketPrice();
			out = new BufferedWriter(new FileWriter(file, true));
			out.append(Integer.toString(price));
			System.out.println(Integer.toString(price) + "+" + (month + 1)
					+ "+" + day);
			out.newLine();

			out.close();

		} catch (IOException e) {
			System.out.println("There was a problem:" + e);
		}
	}

	// Reades the file
	public static int readFile() {
		try {
			in = new BufferedReader(new FileReader(file));
			read = in.readLine();
			temp = read.split("\\+");
			splitIt(temp);

			in.close();
		} catch (IOException e) {
			System.out.println("There was a problem:" + e);
		}
		return startPrice;
	}

	public static void splitIt(String[] s) {
		System.out.println("------------");
		System.out.println("Start price is/was: " + s[0]);
		System.out.println("Start date is/was: " + s[1] + "-" + s[2]);
		System.out.println("------------");
		//System.out.println("Price: " + s[3]);
		//System.out.println("Date: " + s[4] + "-" + s[5]);
	}

}
